#!/bin/bash

# 1
source overcloudrc
Subnets="$(openstack network list -f csv | grep -v lb-mgmt-net | awk -F\' '{print $2}')"
for i in $Subnets; do
  openstack subnet delete $i
done
openstack network delete \
  production-network1 finance-network1 \
  provider-datacentre provider-storage

openstack domain create Operations
openstack project create Engineering --domain Operations

openstack user create --project Engineering --domain Operations --password Flectrag1 Robert1
openstack user create --project Engineering --domain Operations --password Flectrag1 George1
openstack user create --project Engineering --domain Operations --password Flectrag1 William1

openstack role add --project Engineering --project-domain Operations --user-domain Operations --user Robert1 admin
openstack role add --project Engineering --project-domain Operations --user-domain Operations --user George1 admin
openstack role add --project Engineering --project-domain Operations --user-domain Operations --user William1 member

cp overcloudrc robertrc
sed -i -e 's/OS_USERNAME=admin/OS_USERNAME=Robert1/g' robertrc
sed -i -e 's/OS_PROJECT_NAME=admin/OS_PROJECT_NAME=Engineering/g' robertrc
sed -i -e 's/OS_USER_DOMAIN_NAME=Default/OS_USER_DOMAIN_NAME=Operations/g' robertrc
sed -i -e 's/OS_PROJECT_DOMAIN_NAME=Default/OS_PROJECT_DOMAIN_NAME=Operations/g' robertrc
sed -i -e 's/OS_PASSWORD=redhat/OS_PASSWORD=Flectrag1/g' robertrc
sed -i -e '22,28d' robertrc
echo "export PS1='(Robert1-Engineering)[\u@\h \W]$ '" >> robertrc

# 6
## 准备 NFS 存储
mkdir backup_yaml
cat > backup_yaml/nfs-inventory.ini <<EOF
[BACKUP_NODE]
backup ansible_host=172.25.250.220 ansible_user=student ansible_become_password=student
EOF
cat > backup_yaml/bar_nfs_setup.yaml <<EOF
- become: true
  hosts: backup
  name: Setup NFS server for ReaR
  roles:
  - role: backup-and-restore
EOF
### 配置 utility 节点 NFS 共享
ansible-playbook -i backup_yaml/nfs-inventory.ini \
  -t bar_setup_nfs_server \
  backup_yaml/bar_nfs_setup.yaml
ssh root@utility systemctl disable --now firewalld
## 在 Director 节点上安装 ReaR
source stackrc
tripleo-ansible-inventory \
  --ansible_ssh_user heat-admin \
  --static-yaml-inventory backup_yaml/tripleo-inventory.yaml
cat > backup_yaml/bar_rear_setup-undercloud.yaml<<EOF
- become: true
  hosts: undercloud
  name: Install ReaR
  roles:
  - role: backup-and-restore
EOF
ansible-playbook \
  -i backup_yaml/tripleo-inventory.yaml \
  -t bar_setup_rear \
  backup_yaml/bar_rear_setup-undercloud.yaml
## 在 Overcloud 节点上安装 ReaR
cat > backup_yaml/bar_rear_setup-controller.yaml<<EOF
- become: true
  hosts: Controller
  name: Install ReaR
  roles:
  - role: backup-and-restore
EOF
ansible-playbook \
  -i backup_yaml/tripleo-inventory.yaml \
  -e tripleo_backup_and_restore_exclude_paths_controller_non_bootrapnode=false \
  -t bar_setup_rear \
  backup_yaml/bar_rear_setup-controller.yaml

# 7
source robertrc
openstack server create \
  --host computehci0.overcloud.example.com \
  --flavor m1.petite \
  --image web \
  --nic net-id=engnet \
  --security-group ssh \
  --security-group web \
  --key-name webkey \
  --wait \
  rbserver

# 8
cat > ~stack/cloud-init.sh <<EOF
#!/usr/bin/bash
HOSTNAME=\$(hostname -s)
yum -y install httpd
echo "Open sesame" > /var/www/html/index.html
systemctl enable --now httpd
EOF

# 10
sshpass -p redhat ssh root@localhost "
    mkdir /home/stack/materials/
    wget http://materials/heat/labserver-with-floatingip.yaml -O /home/stack/materials/web_server.yaml
    sed -i -e '13s/key_name/key_nam/' -e '19s/ m/m/' /home/stack/materials/web_server.yaml
    "

# 14
source robertrc 
openstack role create Engineers
openstack aggregate create Outspread

# 18
cat > manila-data.sh << EOF
#!/bin/bash
cat > /etc/sysconfig/network-scripts/ifcfg-eth1 << eof
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=dhcp
eof
ifup eth1
curl http://materials.example.com/ceph.repo -o /etc/yum.repos.d/ceph.repo
yum -y install ceph-fuse
EOF

# 20
source overcloudrc
openstack flavor create m1.tiny

# 21
source overcloudrc
openstack project delete finance
source robertrc
openstack domain create Sales
openstack project create Finance --domain Sales
openstack user create Takeshi --domain Sales
openstack user create Francesco --domain Sales
